package thread;

/**
 * @description: 模拟多线程下载功能
 * 问题分析：
 * 1、共享数据：文件
 * 2、
 * @author: apple
 * @date:  2021/11/29 10:44 下午
 * @version 1.0
 **/
public class DownloadTest {

    public static void main(String[] args) {
        Task data = new Task();
        Thread t1 = new Thread(data , "线程1");
        Thread t2 = new Thread(data , "线程2");
        System.out.println("开始下载文件" );
        t1.start();
        t2.start();
    }
}

class Task implements Runnable {

    // 文件初始大小KB
    private final int fileSize = 1000;
    // 下载限速KB
    private int step = 20;
    // 总共下载的大小
    private int total = 0;

    @Override
    public void run() {
        do{

            synchronized(this) {
                this.notify();
                if (total < fileSize) {
                    total += step;
                    System.out.println(Thread.currentThread().getName() + ":已下载：" + total + "KB");

                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    System.out.println(Thread.currentThread().getName() + "文件下载完毕！");
                    break;
                }
            }
        }while (true);

    }
}


